package Q519;

import java.util.*;
import java.util.logging.Handler;

/**
 * 随机反转矩阵
 *
 * @author 23737
 * @time 2021.11.27
 */
public class Test {
    public static void main(String[] args) {
        Solution obj = new Solution(3, 1);
        int[] param_1 = obj.flip();
        System.out.println("the first");
        for (int i : param_1) {
            System.out.println(i);
        }
        obj.reset();

        System.out.println("second");
        int[] param_2 = obj.flip();
        for (int i : param_2) {
            System.out.println(i);
        }
        obj.reset();

        System.out.println("third");
        int[] param_3 = obj.flip();
        for (int i : param_3) {
            System.out.println(i);
        }
        obj.reset();
    }
}

/**
 * 我觉得我的答案很完美
 * 思路什么的都是在正确的，但是一个样例也过不了
 */
class Solution {

    private final int[][] result;
    private final int[] queryArray = new int[2];
    private final Map<Integer, Integer> map = new HashMap<>();
//    private List<Map> list = new ArrayList<>();

    public Solution(int m, int n) {
        result = new int[m][n];

        //init 0
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                result[i][j] = 0;
            }
        }
    }

    public int[] flip() {
        //store
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                if (result[i][j] == 0) {
                    map.put(i, j);
//                    list.add(map);
                    result[i][j] = 1;
                }
            }
        }

//        System.out.println("map size --> " + map.size());
//        System.out.println("traverse map");
//        map.forEach((k, v) -> {
//            System.out.println("k->" + k);
//            System.out.println("v->" + v);
//        });

        //judge
        Random random = new Random();
        //get random number
        int i = random.nextInt(map.size());
//        System.out.println("random number -->" + i);

        queryArray[0] = i;
        queryArray[1] = map.get(i);

//        System.out.println("k ---> " + queryArray[0]);
//        System.out.println("v ---> " + queryArray[1]);
        return queryArray;
    }

    public void reset() {
        //reset 0
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                result[i][j] = 0;
            }
        }
        map.clear();
    }
}
